package cn.yedong.org.controller;

import cn.yedong.basic.util.AjaxResult;
import cn.yedong.basic.util.QueryResult;
import cn.yedong.org.domain.Department;
import cn.yedong.org.query.DepartmentQuery;
import cn.yedong.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value="部门接口类",description = "处理部门的CRUD操作.")
//复合注解：Controller+ResponseBody
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 部门：查询单个
     *
     * @param id
     * @return
     */
    //前端：Get http://localhost:8080/department/id
    @ApiOperation("接口：查询单个")
    @GetMapping("/{id}")
    public Department loadOne(@PathVariable("id") Long id) {
        return departmentService.loadById(id);
    }

    /**
     * 部门:查询全部
     *
     * @return
     */
    //前端：Get http://localhost:8080/department
    @ApiOperation("接口：查询全部")
    @GetMapping
    public List<Department> loadAll() {
        return departmentService.loadAll();
    }

    /**
     * 部门：删除
     * @param id
     */
    //前端：delete http://localhost:8080/department/id
    @ApiOperation("接口：删除")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 部门：添加或修改
     * @param department
     * @return
     */
    //前端：Put http://localhost:8080/department json对象
    @ApiOperation("接口：添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.add(department);
            } else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * 分页查询
     * @param departmentQuery
     * @return
     */
    //前端：Post http://localhost:8080/department json对象
    @ApiOperation("接口：分页查询")
    @PostMapping
    public QueryResult<Department> findPage(@RequestBody DepartmentQuery departmentQuery){
        QueryResult<Department> departmentQueryResult = departmentService.pageQuery(departmentQuery);
        List<Department> data = departmentQueryResult.getData();
        data.forEach(System.out::println);

        return departmentService.pageQuery(departmentQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    //前端：Patch http://localhost:8080/department
    @ApiOperation("接口：批量删除")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            departmentService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败！");
        }
    }

    //前端：get http://localhost:8080/department/deptTree
    @GetMapping("deptTree")
    public List<Department> deptTree(){
        return departmentService.deprTree();
    }
}

